/*
 *	MIDAS Context Based Routing
 *	scheduler.h
 *	Copyright (c) 2007 Bartlomiej Zolnierkiewicz
 *	MEAG IT PW
 *
 *	Loosely based on NS2 scheduler implementation.
 */

#ifndef _scheduler_h
#define _scheduler_h

typedef int scheduler_uid_t;

class Handler;

class Event {
public:
	Event *next_;		// event list
	Handler *handler_;	// handler to call when event ready
	unsigned long time_;	// time at which event is ready
	scheduler_uid_t uid_;	// unique ID

	Event() : time_(0), uid_(0) {}
};

class Handler {
public:
	virtual ~Handler () {}

	virtual void handle(Event* event) = 0;
};

class Scheduler {
public:
	static Scheduler& instance() {
		return (*instance_);		// general access to scheduler
	}
	void schedule(Handler *, Event *, double delay); // schedule later event
	virtual void insert(Event *) = 0;	// schedule event

protected:
	void dispatch(Event *, unsigned long);	// execute event

	virtual ~Scheduler();

	static Scheduler* instance_;
	static scheduler_uid_t uid_;
};

class ListScheduler : public Scheduler {
public:
	ListScheduler() : queue_(0) {}

	void run();
	void insert(Event *);

protected:
	Event *queue_;
};

unsigned long sclock(void);

#endif
